package com.ggb.grpc;

import com.ggb.proto.*;
import com.ggb.proto.StudentServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;

import java.time.LocalDateTime;
import java.util.Iterator;

public class ProtoClientTest {
    public static void main(String[] args) throws InterruptedException {
        ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 8899).usePlaintext().build();
//        StudentServiceGrpc.StudentServiceBlockingStub blockingStub = StudentServiceGrpc.newBlockingStub(managedChannel);
        StudentServiceGrpc.StudentServiceStub studentServiceStub = StudentServiceGrpc.newStub(managedChannel);
//        MyResponse myResponse = blockingStub.getRealNameByUserName(MyRequest.newBuilder().setUsername("zhang san").build());
//        System.out.println("MyResponse Name " + myResponse.getRealname());
//        Iterator<StudentResponse> studentByAge = blockingStub.getStudentByAge(StudentRequest.newBuilder().setAge(20).build());
//        while (studentByAge.hasNext()) {
//            StudentResponse next = studentByAge.next();
//            System.out.println(next.getName() + " " + next.getCity());
//        }
        StreamObserver<StudentResponseList> studentResponseListStreamObserver = new StreamObserver<StudentResponseList>() {
            @Override
            public void onNext(StudentResponseList value) {
                value.getStudentResponseList().forEach(studentResponse -> {
                    System.out.println(studentResponse.getName() + " " + studentResponse.getCity());
                });
            }

            @Override
            public void onError(Throwable t) {
                System.out.println("错误");
            }

            @Override
            public void onCompleted() {
                System.out.println("完成");
            }
        };

//        // 异步执行
//        StreamObserver<StudentRequest> studentWrapperByAges = studentServiceStub.getStudentWrapperByAges(studentResponseListStreamObserver);
//        studentWrapperByAges.onNext(StudentRequest.newBuilder().setAge(20).build());
//        studentWrapperByAges.onNext(StudentRequest.newBuilder().setAge(30).build());
//        studentWrapperByAges.onNext(StudentRequest.newBuilder().setAge(40).build());
//        studentWrapperByAges.onNext(StudentRequest.newBuilder().setAge(50).build());
//
//        studentWrapperByAges.onCompleted();

        StreamObserver<StreamRequest> streamRequestStreamObserver = studentServiceStub.biTalk(new StreamObserver<StreamResponse>() {
            @Override
            public void onNext(StreamResponse value) {
                System.out.println(value.getResponseInfo());
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.getMessage());

            }

            @Override
            public void onCompleted() {
                System.out.println("completed!");
            }
        });
        for (int i = 0; i < 10; i++) {
            streamRequestStreamObserver.onNext(StreamRequest.newBuilder().setRequestInfo(LocalDateTime.now().toString()).build());
            Thread.sleep(1000);
        }
        Thread.sleep(5000);
    }
}
